package com.sziit.se.highconcurrency.user.application.controller;

import com.sziit.se.highconcurrency.common.annotation.IgnoreCheck;
import com.sziit.se.highconcurrency.common.constant.Constants;
import com.sziit.se.highconcurrency.common.constant.SystemConstants;
import com.sziit.se.highconcurrency.common.entity.LoginRsp;
import com.sziit.se.highconcurrency.common.result.Result;
import com.sziit.se.highconcurrency.common.result.ResultUtil;
import com.sziit.se.highconcurrency.common.service.IUserService;
import com.sziit.se.highconcurrency.common.entity.UserInfoVo;
import com.sziit.se.highconcurrency.common.tool.PrintThreadInfo;
import com.sziit.se.highconcurrency.user.application.entity.LoginRequest;
import com.sziit.se.highconcurrency.user.application.entity.RegisterRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserAccountController {

    @Autowired
    @Qualifier("UserService")
    private IUserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody RegisterRequest registerRequest) {
        //只允许普通消费者注册
        String role = SystemConstants.CUSTOMER_ROLE;
        Boolean registered = userService.insertUser(registerRequest.getUserName(), registerRequest.getPassword(), registerRequest.getPhone(),role);
        return ResultUtil.auto(registered);
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) throws InterruptedException {
        LoginRsp loginRsp = userService.login(loginRequest.getUserName(), loginRequest.getPassword());
        PrintThreadInfo.printThreadInfo();
        return ResultUtil.data(loginRsp);
    }

    @GetMapping("/userInfo")
    public Result userInfo() {
        UserInfoVo userInfoVo = userService.userInfo();
        return ResultUtil.data(userInfoVo);
    }



    @PutMapping("/userInfo")
    public Result updateUser(@RequestBody RegisterRequest registerRequest) {
        UserInfoVo userInfoVo = userService.updateUser(registerRequest.getUserName(), registerRequest.getPassword(), registerRequest.getPhone());
        return ResultUtil.data(userInfoVo);
    }
    @GetMapping
    @IgnoreCheck
    @RequestMapping("/query-by-name")
    public Result getUserByName(@RequestParam(value = "userName") String userName) throws InterruptedException {
        return ResultUtil.data(userService.getByName(userName));
    }


    @GetMapping
    @IgnoreCheck
    @RequestMapping("/query-by-id")
    public Result getUserById(@RequestParam(value = "userId") String userId) throws InterruptedException {
        return ResultUtil.data(userService.getById(userId));
    }
}
